#include <sourcemod>
#include <sdktools>
#include <cstrike>

#define PLUGIN_VERSION "1.0.0"

new Handle:CvarBonusMode;
new FragsFromTSpawnCount[MAXPLAYERS+1]

public Plugin:myinfo = 
{
	name = "No Frags From T Spawn",
	author = "-sang-",
	description = "Disable frags from T spawn",
	version = PLUGIN_VERSION,
	url = "http://www.css-quad.ru/"
}

public OnPluginStart()
{
	CvarBonusMode = CreateConVar("sm_nffts_bonus_mode", "1", "Add frags from T spawn if T win", 0, true, 0.0, true, 1.0);

	HookEvent("player_death", Event_PlayerDeath, EventHookMode_Pre)
	HookEvent("round_start", Event_RoundStart)
	HookEvent("round_end", Event_RoundEnd)
}

public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"))
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"))
	if (attacker > 0
		&& (GetClientTeam(attacker) != GetClientTeam(client))
		&& IsPlayerOnTSpawn(attacker))
	{
		FragsFromTSpawnCount[attacker]++
		if (IsBonusMode())
		{
			DecreasePlayerFrags(attacker, 1)
		}
	}
}

public Action:Event_RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
	for (new client = 1; client <= MaxClients; client++)
	{
		FragsFromTSpawnCount[client] = 0
	}
}

public Action:Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
{
	new winnerTeam = GetEventInt(event, "winner")
	if (winnerTeam == CS_TEAM_CT
		&& !IsBonusMode())
	{
		RemoveAllFragsFromTSpawn()
	}
	else if (winnerTeam == CS_TEAM_T
		&& IsBonusMode())
	{
		AddAllBonusFrags()
	}
}

bool:IsBonusMode()
{
	return GetConVarInt(CvarBonusMode) == 1
}

AddAllBonusFrags()
{
	for (new client = 1; client <= MaxClients; client++)
	{
		AddFragsFromTSpawn(client)
	}
}

RemoveAllFragsFromTSpawn()
{
	for (new client = 1; client <= MaxClients; client++)
	{
		RemoveFragsFromTSpawn(client)
	}
}

RemoveFragsFromTSpawn(client)
{
	new lostFragsCount = FragsFromTSpawnCount[client]
	if (lostFragsCount > 0)
	{
		PrintToChat(client, "\x04[\x03NFFTS\x04] \x03You lost %i kills", lostFragsCount)
		DecreasePlayerFrags(client, lostFragsCount)
	}
}

AddFragsFromTSpawn(client)
{
	new bonusFragsCount = FragsFromTSpawnCount[client]
	if (bonusFragsCount > 0)
	{
		PrintToChat(client, "\x04[\x03NFFTS\x04] \x03You get %i bonus kills", bonusFragsCount)
		IncreasePlayerFrags(client, bonusFragsCount)
	}
}

IncreasePlayerFrags(client, fragsCount)
{
	new currentFragsCount = GetClientFrags(client)
	SetEntProp(client, Prop_Data, "m_iFrags", currentFragsCount + fragsCount)
}

DecreasePlayerFrags(client, fragsCount)
{
	new currentFragsCount = GetClientFrags(client)
	SetEntProp(client, Prop_Data, "m_iFrags", currentFragsCount - fragsCount)
}

bool:IsPlayerOnTSpawn(client)
{
	new bool:result = true
	decl Float:playerOrigin[3]
	GetClientAbsOrigin(client, playerOrigin)
	result &= IsPointBetweenCoordinates(playerOrigin, -674.0, -1073.0, 0.0, -221.0, -391.0, 361.0)
	return result
}

bool:IsPointBetweenCoordinates(const Float:pointposition[3], Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2)
{	
	return (pointposition[0] > x1 && pointposition[0] < x2)
		&& (pointposition[1] > y1 && pointposition[1] < y2)
		&& (pointposition[2] > z1 && pointposition[2] < z2)
}